import csv
import os

from lib.config.config import Config
import redis

from lib.config.redis_config import redisconfig


class RedisOperate:

    def __init__(self):
        config = redisconfig()
        host = config.host
        port = config.port
        db = config.db
        password = config.password
        if len(password) == 0:
            print("no password")
            self.redisclient = redis.Redis(host=host, port=port, db=db, password=None, decode_responses=True)
        else:
            self.redisclient = redis.Redis(host=host, port=port, db=db, password=password, decode_responses=True)
        ok = self.redisclient.ping()
        print(ok)

    def getdata(self):
        data = self.redisclient.get("register_from_douyin")
        print("----------------------111-----------")
        print(data)

    def setdata(self):
        path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/conf/phone.csv"
        key = "lovely_invite_rank"

        with open(path, encoding='utf-8') as f:
            reader = csv.reader(f)
            for row in reader:
                print(row[0], "===", row[1])
                self.redisclient.zadd(key, mapping={row[0]: row[1]})

    # def settest(self):
    #     phone = "15296235982"
    #     score = 130
    #     self.redisclient.zadd("lovely_invite_rank", mapping={
    #         phone: score
    #     })
